package com.gbb.common.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import androidx.viewpager.widget.PagerAdapter;

/**
 * 通用的ViewPager适配器
 *
 * @author gaobingbing
 * @see AbstractPagerAdapter
 */
@Deprecated
public abstract class SimplePagerAdapter<T> extends PagerAdapter {

	protected Context mContext;

	protected List<T> dataList;

	private final List<View> viewList;

	public SimplePagerAdapter(Context mContext,List<T> dataList) {
		this.mContext = mContext;
		this.dataList = dataList;
		viewList = new ArrayList<>();
		createChildItem(viewList);
	}

	public void setDataList(List<T> dataList) {
		this.dataList = dataList;
		createChildItem(viewList);
		notifyDataSetChanged();
	}

	/**
	 * 将数据列表转换为View列表
	 *
	 * @param viewList 已实例化
	 */
	public abstract void createChildItem(List<View> viewList);

	@Override
	public int getCount() {
		return viewList == null ? 0 : viewList.size();
	}

	@Override
	public boolean isViewFromObject(View view,Object object) {
		return view == object;
	}

	@Override
	public Object instantiateItem(ViewGroup container,int position) {
		if(viewList.size() == 0) {
			return null;
		}
		int newPosition = position%viewList.size();
		View childView = viewList.get(newPosition);

		// ViewGroup childParent = (ViewGroup) childView.getParent();
		// if (childParent != null) {
		// childParent.removeView(childView);
		// }

		container.addView(childView);
		return childView;
	}

	@Override
	public void destroyItem(ViewGroup container,int position,Object object) {
		container.removeView(viewList.get(position%viewList.size()));
		// 待检测 container.removeView((View)object);
	}

	/**
	 * 很重要，否则不能notifyDataSetChanged.
	 */
	@Override
	public int getItemPosition(Object object) {
		return POSITION_NONE;
	}

}
